home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-nt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-18  |  5.5 KB  |  285 lines

  1. /*  $Id: pl-nt.c,v 1.20 1998/02/18 13:57:06 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: Windows (NT) specific stuff
  8. */
  9.  
  10. #if defined(__WINDOWS__) || defined(__WIN32__)
  11.  
  12. #include <windows.h>
  13. #include <process.h>
  14. #include "pl-incl.h"
  15. #include "pl-ctype.h"
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include "pl-stream.h"
  19. #include <console.h>
  20. #include <process.h>
  21.  
  22.  
  23.          /*******************************
  24.          *      DUMMY EXTENSION    *
  25.          *******************************/
  26.  
  27. PL_extension PL_extensions [] =
  28. {
  29. /*{ "name",    arity,  function,    PL_FA_<flags> },*/
  30.  
  31.   { NULL,    0,     NULL,        0 }    /* terminating line */
  32. };
  33.  
  34.          /*******************************
  35.          *        MESSAGE BOX        *
  36.          *******************************/
  37.  
  38. void
  39. PlMessage(const char *fm, ...)
  40. { va_list(args);
  41.   char buf[1024];
  42.  
  43.   va_start(args, fm);
  44.   vsprintf(buf, fm, args);
  45.   va_end(args);
  46.  
  47.   MessageBox(NULL, buf, "SWI-Prolog", MB_OK|MB_TASKMODAL);
  48. }
  49.  
  50.          /*******************************
  51.          *           WIN32        *
  52.          *******************************/
  53.  
  54. int
  55. iswin32s()
  56. { if( GetVersion() & 0x80000000 && (GetVersion() & 0xFF) ==3)
  57.     return TRUE;
  58.   else
  59.     return FALSE;
  60. }
  61.  
  62.  
  63.          /*******************************
  64.          *    WinAPI ERROR CODES    *
  65.          *******************************/
  66.  
  67. char *
  68. WinError()
  69. { int id = GetLastError();
  70.   char *msg;
  71.   static WORD lang;
  72.   static lang_initialised = 0;
  73.  
  74.   if ( !lang_initialised )
  75.     lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK);
  76.  
  77. again:
  78.   if ( FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
  79.              FORMAT_MESSAGE_IGNORE_INSERTS|
  80.              FORMAT_MESSAGE_FROM_SYSTEM,
  81.              NULL,            /* source */
  82.              id,            /* identifier */
  83.              lang,
  84.              (LPTSTR) &msg,
  85.              0,                /* size */
  86.              NULL) )            /* arguments */
  87.   { atom_t a = lookupAtom(msg);
  88.  
  89.     LocalFree(msg);
  90.     lang_initialised = 1;
  91.  
  92.     return stringAtom(a);
  93.   } else
  94.   { if ( lang_initialised == 0 )
  95.     { lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
  96.       lang_initialised = 1;
  97.       goto again;
  98.     }
  99.  
  100.     return "Unknown Windows error";
  101.   }
  102. }
  103.  
  104.  
  105.          /*******************************
  106.          *      SLEEP/1 SUPPORT    *
  107.          *******************************/
  108.  
  109. void
  110. Pause(double t)
  111. { DWORD msecs = (DWORD)(t * 1000.0);
  112.  
  113.   Sleep(msecs);
  114. }
  115.  
  116.          /*******************************
  117.          *     QUERY CPU TIME        *
  118.          *******************************/
  119.  
  120. #define nano * 0.0000001
  121. #define ntick 1.0            /* manual says 100.0 ??? */
  122.  
  123. real
  124. CpuTime()
  125. { real t;
  126.   HANDLE proc = GetCurrentProcess();
  127.   FILETIME created, exited, kerneltime, usertime;
  128.  
  129.   if ( GetProcessTimes(proc, &created, &exited, &kerneltime, &usertime) )
  130.   { t = (real)usertime.dwHighDateTime * (4294967296.0 * ntick nano);
  131.     t += (real)usertime.dwLowDateTime  * (ntick nano);
  132.   } else                /* '95, Windows 3.1/win32s */
  133.   { extern long clock_wait_ticks;
  134.  
  135.     t = (real) (clock() - clock_wait_ticks) / (real) CLOCKS_PER_SEC;
  136.   }
  137.  
  138.   return t;
  139. }
  140.  
  141.  
  142. char *
  143. Symbols(char *syms)
  144. { int n;
  145.   char buf[MAXPATHLEN];
  146.  
  147.   if ( (n = GetModuleFileName(NULL, buf, sizeof(buf))) > 0 )
  148.   { char buf2[MAXPATHLEN];
  149.  
  150.     buf[n] = EOS;
  151.     _xos_long_file_name(buf, buf2);
  152.  
  153.     strcpy(syms, buf2);
  154.   } else
  155.   { PrologPath(GD->cmdline.argv[0], buf);
  156.  
  157.     strcpy(syms, buf);
  158.   }
  159.  
  160.   return syms;
  161. }
  162.  
  163.          /*******************************
  164.          *     SUPPORT FOR SHELL/2    *
  165.          *******************************/
  166.  
  167. static char *
  168. win_exec(const char *cmd, const char *how)
  169. { UINT show;
  170.   char *msg;
  171.   int rval;
  172.  
  173.   if ( streq(how, "iconic") )
  174.     show = SW_MINIMIZE;
  175.   else /*if ( streq(how, "normal") )*/
  176.     show = SW_NORMAL;
  177.  
  178.   switch((rval = WinExec(cmd, show)))
  179.   { case 0:
  180.       msg = "Not enough memory";
  181.       break;
  182.     case ERROR_BAD_FORMAT:
  183.       msg = "Bad format";
  184.       break;
  185.     case ERROR_FILE_NOT_FOUND:
  186.       msg = "File not found";
  187.       break;
  188.     case ERROR_PATH_NOT_FOUND:
  189.       msg = "Path not found";
  190.       break;
  191.     default:
  192.       if ( rval > 31 )
  193.     msg = NULL;
  194.       else
  195.     msg = "Unknown error";
  196.       break;
  197.   }
  198.  
  199.   return msg;
  200. }
  201.  
  202.  
  203. int
  204. System(char *command)
  205. { STARTUPINFO sinfo;
  206.   PROCESS_INFORMATION pinfo;
  207.   int shell_rval;
  208.  
  209.   memset(&sinfo, 0, sizeof(sinfo));
  210.   sinfo.cb = sizeof(sinfo);
  211.  
  212.   if ( CreateProcess(NULL,            /* module */
  213.              command,            /* command line */
  214.              NULL,            /* Security stuff */
  215.              NULL,            /* Thread security stuff */
  216.              FALSE,            /* Inherit handles */
  217.              NORMAL_PRIORITY_CLASS,    /* flags */
  218.              NULL,            /* environment */
  219.              NULL,            /* CWD */
  220.              &sinfo,            /* startup info */
  221.              &pinfo) )            /* process into */
  222.   { BOOL rval;
  223.     DWORD code;
  224.  
  225.     CloseHandle(pinfo.hThread);            /* don't need this */
  226.       
  227.     do
  228.     { MSG msg;
  229.  
  230.       if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
  231.       { TranslateMessage(&msg);
  232.     DispatchMessage(&msg);
  233.       } else
  234.     Sleep(50);
  235.  
  236.       rval = GetExitCodeProcess(pinfo.hProcess, &code);
  237.     } while(rval == TRUE && code == STILL_ACTIVE);
  238.  
  239.     shell_rval = (rval == TRUE ? code : -1);
  240.     CloseHandle(pinfo.hProcess);
  241.   } else
  242.     return shell_rval = -1;
  243.  
  244.   return shell_rval;
  245. }
  246.  
  247.  
  248. word
  249. pl_win_exec(term_t cmd, term_t how)
  250. { char *s;
  251.   char *h;
  252.  
  253.   if ( PL_get_chars(cmd, &s, CVT_ALL) &&
  254.        PL_get_atom_chars(how, &h) )
  255.   { char *msg = win_exec(s, h);
  256.  
  257.     if ( msg )
  258.       return warning("win_exec/2: %s", msg);
  259.     else
  260.       succeed;
  261.   } else
  262.     return warning("win_exec/2: instantiation fault");
  263. }
  264.  
  265.  
  266. char *
  267. getenv3(const char *name, char *buf, int len)
  268. { if ( GetEnvironmentVariable(name, buf, len) )
  269.     return buf;
  270.   
  271.   return NULL;
  272. }
  273.  
  274. /* What does this return if the variable is not defined?
  275. */
  276.  
  277. int
  278. getenvl(const char *name)
  279. { return GetEnvironmentVariable(name, NULL, 0);
  280. }
  281.  
  282. #endif /*__WINDOWS__*/
  283.  
  284.  
  285.